﻿import java.util.Iterator;
public class Shape {
	private java.util.List<Line> lines;

	public void removeAllLines() {
		if (lines != null)
			lines.clear();
	}

	public java.util.List<Line> getLines() {
		if (lines == null)
			lines = new java.util.ArrayList<Line>();
		return lines;
	}

	public void setLines(java.util.List<Line> newLines) {
		removeAllLines();
		for (Iterator<Line> iter = newLines.iterator(); iter.hasNext();)
			addLines((Line) iter.next());
	}

	public void addLines(Line newLine) {
		if (newLine == null)
			return;
		if (this.lines == null)
			this.lines = new java.util.ArrayList<Line>();
		if (!this.lines.contains(newLine))
			this.lines.add(newLine);
	}

	/**
	 * 随机画多边形，startPoint记录下一条线的起点，默认值为null；endPoint记录下一条线的终点，默认值为null。
	 */
	public void drawShape(int edgeNum) {
//		Line line = new Line();
//		Point point = new Point();
//		int num = 2;
//		if (num < edgeNum) {
//			line.drawLine(line.getStartPoint(), line.getEndPoint());
//			// point
//			line.setStartPoint(line.getEndPoint());
//			--edgeNum;
//			if (num == edgeNum - 2) {
//				line.setEndPoint(getFirstPoint());
//			}
//			addLines(line);
//		}
		Point startPoint = null;
		Point endPoint = null;
		for (int i = 0; i < edgeNum; i++) {
			Line line = new Line().drawLine(startPoint, endPoint);
			startPoint = line.getEndPoint();
			if (i == edgeNum - 2) {
				endPoint = this.getFirstPoint();
			}
			this.addLines(line);
		}
	}

	/**
	 * 主函数
	 */
	public static void main(String[] args) {
		Shape shape = new Shape();
		int edgeNum = 5;
		shape.drawShape(edgeNum);

		for (Line line : shape.lines) {
			Point spoint = line.getStartPoint();
			Point epoint = line.getEndPoint();
			System.out.println("line point:" + spoint.getX() + "-" + spoint.getY() + "、" + epoint.getX() + "-" + epoint.getY());
		}
	}

	/**
	 * 获得画多边形的第一点：lines(0).getStartPoint()
	 */
	public Point getFirstPoint() {
		if (this.lines != null && this.lines.size() > 0) 
		return lines.get(0).getStartPoint();
		return null;
	}
}